﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.service;
using DXApplicationTangche.UC.面料审核.Service;
using DXApplicationTangche.UC.面料审核.生产计划UC;
using DXApplicationTangche.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.面料审核
{
    public partial class Form生产计划单产成品 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private String shopId { get; set; }
        private String shopName { get; set; }
        //出入库DTO
        private List<GarmentStockDTO> garmentStockDTOs = new List<GarmentStockDTO>();
        //生产计划视图DTO
        private MrpInventoryCalculateResultDTO mrpInventoryCalculateResultDTO = new MrpInventoryCalculateResultDTO();
        //UC
        private XtraUserControl生产计划视图 xtraUserControl生产计划视图 = new XtraUserControl生产计划视图();
        private XtraUserControl每日进出库明细 xtraUserControl每日进出库明细 = new XtraUserControl每日进出库明细();

        public Form生产计划单产成品()
        {
            InitializeComponent();
            this.searchLookUpEdit选择店铺.Properties.DataSource = UserPrivilegesService.getShopRightQueryFacadeByOrgId("shunqiu").shopRightQueryFacadeDTOList;
        }
        #region 选择店铺
        private void searchLookUpEdit选择店铺_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit选择店铺View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit选择店铺.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.shopId = this.searchLookUpEdit选择店铺.Properties.View.GetRowCellValue(rowHandle, "shopId").ToString();
                    this.shopName = this.searchLookUpEdit选择店铺.Properties.View.GetRowCellValue(rowHandle, "shopName").ToString();
                }
            }
            if(!String.IsNullOrEmpty(this.dateTimePicker开始时间.Value.ToString("yyyy-MM-dd"))
                && !String.IsNullOrEmpty(this.dateTimePicker开始时间.Value.ToString("yyyy-MM-dd")))
            {
                //this.刷新();
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit选择店铺.ToolTip = null;
            searchLookUpEdit选择店铺.EditValue = null;
        }
        private void searchLookUpEdit选择店铺_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.shopName;
        }
        #endregion

        private void dateTimePicker开始时间_CloseUp(object sender, EventArgs e)
        {
            this.刷新();
        }

        private void dateTimePicker结束时间_CloseUp(object sender, EventArgs e)
        {
            this.刷新();
        }
        private void 刷新()
        {
            if (String.IsNullOrEmpty(this.shopId))
            {
                MessageBox.Show("请选择门店");
                return;
            }
            this.splashScreenManager.ShowWaitForm();
            this.splashScreenManager.SetWaitFormCaption("请稍后,正在加载中....");     // 标题
            this.splashScreenManager.SetWaitFormDescription("正在初始化.....");     // 信息
            //出入库一览
            this.garmentStockDTOs = null;
            this.garmentStockDTOs = 面料审核Service.getStockInOutDetail(
                this.shopId,
                this.dateTimePicker开始时间.Value.ToString("yyyy-MM-dd"),
                this.dateTimePicker结束时间.Value.ToString("yyyy-MM-dd"));
            //生产计划视图
            this.mrpInventoryCalculateResultDTO = null;            
            this.mrpInventoryCalculateResultDTO = 面料审核Service.getMrpInventoryCalculateResultWithDouyinInfo(
                this.shopId,
                this.dateTimePicker开始时间.Value.ToString("yyyy-MM-dd"),
                this.dateTimePicker结束时间.Value.ToString("yyyy-MM-dd"));
            //加载生产计划视图uc
            this.xtraUserControl生产计划视图 = new XtraUserControl生产计划视图(this.mrpInventoryCalculateResultDTO);
            this.xtraUserControl生产计划视图.Dock = DockStyle.Fill;
            this.panel1.Controls.Clear();
            this.panel1.Controls.Add(this.xtraUserControl生产计划视图);
            this.splashScreenManager.CloseWaitForm();
        }
        /// <summary>
        /// 加载生产计划视图UC
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.xtraUserControl生产计划视图 = new XtraUserControl生产计划视图(this.mrpInventoryCalculateResultDTO);
            this.xtraUserControl生产计划视图.Dock = DockStyle.Fill;
            this.panel1.Controls.Clear();
            this.panel1.Controls.Add(this.xtraUserControl生产计划视图);
        }
        /// <summary>
        /// 加载每日进出库明细UC
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.xtraUserControl每日进出库明细 = new XtraUserControl每日进出库明细(this.garmentStockDTOs);
            this.xtraUserControl每日进出库明细.Dock = DockStyle.Fill;
            this.panel1.Controls.Clear();
            this.panel1.Controls.Add(this.xtraUserControl每日进出库明细);
        }
    }
}
